package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbDict;
import com.bwie.service.TbDictService;
import com.bwie.mapper.TbDictMapper;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author lenovo
 * @description 针对表【tb_dict(字典表)】的数据库操作Service实现
 * @createDate 2025-06-07 11:01:11
 */
@Service
public class TbDictServiceImpl extends ServiceImpl<TbDictMapper, TbDict>
        implements TbDictService {

    @Autowired
    TbDictMapper tbDictMapper;

    @Autowired
    StringRedisTemplate redisTemplate;


    @PostConstruct
    public void loadDict() {
        List<TbDict> tbDictList = tbDictMapper.selectList(null);

        //redis的Hash结构存储
        for (TbDict tbDict : tbDictList) {

            String key = "dict-" + tbDict.getDictType();
            String key2 = tbDict.getDictValue().toString();
            String value = JSON.toJSONString(tbDict);

            redisTemplate.opsForHash().put(key, key2, value);

        }

        System.out.println("加载完毕-****************");

    }


}




